<?php
require_once 'DBUtil.class.php';
require_once 'Admin.class.php';
class AdminAction {
	//创建
	function create($admin) {
		$con = DBUtil::getCon();
		$sql = "INSERT INTO admin (account, password, name, email) VALUES ('{$admin->account}', '{$admin->password}', '" . $admin -> name . "','{$admin -> email}')";
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return mysql_insert_id();
		}
	}
	
	//登录
	function login($admin){
		$con = DBUtil::getCon();
		$sql = "select * from admin where account='{$admin -> account}' and password='{$admin -> password}'";
		$result = mysql_query($sql);
		if (!$result) {
			die('Error: ' . mysql_error());
			return false;
		} else {
			$row = mysql_fetch_array($result);
			$admin -> id = $row['id'];
			$admin -> account = $row['account'];
			$admin -> password = $row['password'];
			$admin -> name = $row['name'];
			$admin -> email = $row['email'];
			return $admin;
		}
	}
	
	//根据ID查询
	function queryById($admin) {
		$con = DBUtil::getCon();
		$sql = "SELECT * FROM admin WHERE id=" . $admin -> id;
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$admin -> account = $row['account'];
		$admin -> password = $row['password'];
		$admin -> name = $row['name'];
		$admin -> email = $row['email'];
		return $admin;
	}

	//查询所有
	function queryAll() {
		$con = DBUtil::getCon();
		$sql = "SELECT * FROM admin";
		$result = mysql_query($sql);
		$admins = array();
		if ($result) {
			while ($row = mysql_fetch_array($result)) {
				$admin = new Admin();
				$admin -> account = $row['account'];
				$admin -> password = $row['password'];
				$admin -> name = $row['name'];
				$admin -> email = $row['email'];
				$admins[] = $admin;
			}
		}
		return $admins;
	}

	//修改
	function save($admin) {
		$con = DBUtil::getCon();
		$sql = "UPDATE admin SET name='{$admin->account}', title={$admin->password}, name ='{$admin->name}' WHERE id={$user->id}";
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return true;
		}
	}

	//删除
	function delete($admin) {
		$con = DBUtil::getCon();
		$sql = "DELETE FROM admin WHERE id=" . $admin -> id;
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return true;
		}
	}

}
?>